package com.ruoyi.system.vo.weishuapay;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.client.ClientProtocolException;
import org.springframework.stereotype.Controller;

import com.ruoyi.common.utils.PropertiesConst;
import com.ruoyi.system.vo.Aes;
import com.ruoyi.system.vo.HttpClientUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
public class CityUtil {

	public static List<RegionBean> getRegion() {
		if (ReadExcelTools.regionList == null || ReadExcelTools.regionList.size() == 0) {
			ReadExcelTools.loadWeishua();
		}
		return ReadExcelTools.regionList;
	}

	public static List<ProductBean> getRegionYX(String cityName, String acqcode) {
		if (acqcode.equals(PropertiesConst.acq_code_yixian_xs8087)) {
			if (yixian_products_map == null || yixian_products_map.size() == 0) {
				loadYxCityAndProducts("0", PropertiesConst.acq_code_yixian_xs8087);
			}
			Set<String> keySet = yixian_products_map.keySet();
			for (String key : keySet) {
				if (key.contains(cityName)) {
					return yixian_products_map.get(key);
				}
			}
			for (String key : keySet) {
				if (cityName.contains(key)) {
					return yixian_products_map.get(key);
				}
			}
		}
		if (acqcode.equals(PropertiesConst.acq_code_yixian_8063)) {
			if (yixian_products_map_51 == null || yixian_products_map_51.size() == 0) {
				loadYxCityAndProducts("0", PropertiesConst.acq_code_yixian_8063);
			}
			Set<String> keySet = yixian_products_map_51.keySet();
			for (String key : keySet) {
				if (key.contains(cityName)) {
					return yixian_products_map_51.get(key);
				}
			}
			for (String key : keySet) {
				if (cityName.contains(key)) {
					return yixian_products_map_51.get(key);
				}
			}
		}
		return null;
	}

	public static List<RegionBean> getRegionKFT() {
		if (ReadExcelTools.regionListKFT == null || ReadExcelTools.regionListKFT.size() == 0) {
			ReadExcelTools.loadKFT();
		}
		return ReadExcelTools.regionListKFT;
	}
	
	public static List<RegionBean> getRegionHD() {
		if (ReadExcelTools.regionListhd == null || ReadExcelTools.regionListhd.size() == 0) {
			ReadExcelTools.loadhd();
		}
		return ReadExcelTools.regionListhd;
	}

	public static List<RegionBean> getRegion(String parentId) {
		List<RegionBean> leve0 = new ArrayList<>();
		for (RegionBean bean : ReadExcelTools.regionList) {
			if (bean.getParentId().equals(parentId)) {
				leve0.add(bean);
			}
		}
		return leve0;
	}

	// 存储yx所有的城市和商户map，定时每天清空一次，就可以获取到新的数据了，不用每次客户制定计划时候选择
	public static Map<String, List<ProductBean>> yixian_products_map = new HashMap<String, List<ProductBean>>();
	public static Map<String, List<ProductBean>> yixian_products_map_51 = new HashMap<String, List<ProductBean>>();

	/**
	 * 获取快付通-省份、城市
	 * 
	 * @param parent
	 * @return
	 */
	public static List<RegionBean> getRegionKFT(String parent) {
		List<RegionBean> leve0 = new ArrayList<>();
		RegionBean addbean;
		if ("0".equals(parent)) {
			for (String p : ReadExcelTools.provicesKFT) {
				addbean = new RegionBean();
				addbean.setParentId("0");
				addbean.setRegionCode(p);
				addbean.setRegionId(p);
				addbean.setRegionName(p);
				leve0.add(addbean);
			}
		} else {
			for (RegionBean _bean : ReadExcelTools.regionListKFT) {
				if (_bean.getRegionCode().equals(parent)) {
					addbean = new RegionBean();
					addbean.setParentId(_bean.getRegionCode());
					addbean.setRegionCode(_bean.getRegionCode());
					addbean.setRegionId(_bean.getRegionCode());
					addbean.setRegionName(_bean.getRegionName());
					leve0.add(addbean);
				}
			}
		}
		return leve0;
	}

	/**
	 * 获取快付通-具体区域
	 * 
	 * @param parent
	 * @return
	 */
	public static List<RegionBean> getRegionKFTDist(String parent) {
		List<RegionBean> leve0 = new ArrayList<>();
		RegionBean addbean;
		for (RegionBean _bean : ReadExcelTools.regionListKFT) {
			if (_bean.getRegionName().equals(parent)) {
				addbean = new RegionBean();
				addbean.setParentId(_bean.getRegionName());
				addbean.setRegionCode(_bean.getRegionName());
				addbean.setRegionId(_bean.getRegionId());
				addbean.setRegionName(_bean.getParentId());
				leve0.add(addbean);
			}
		}
		return leve0;
	}

	public static List<FrontCityBean> region2Front(List<RegionBean> list) {
		List<FrontCityBean> leve0 = new ArrayList<>();
		FrontCityBean front;
		for (RegionBean bean : list) {
			front = new FrontCityBean();
			front.setAreaCode(bean.getRegionCode());
			front.setChinaRegionId(bean.getRegionId());
			front.setDivisionCode(bean.getRegionCode());
			front.setDivisionName(bean.getRegionName());
			front.setId(bean.getRegionId());
			front.setLatitude("");
			front.setLongitude("");
			front.setRanking(bean.getRegionType());
			front.setParentId(bean.getParentId());
			leve0.add(front);
		}
		return leve0;
	}

	//刷新城市
	public static void loadYxCityAndProducts(String parentId, String acqcode) {
		try {
			if (acqcode.equals(PropertiesConst.acq_code_yixian_xs8087))
				yixian_products_map.clear();
			if (acqcode.equals(PropertiesConst.acq_code_yixian_8063))
				yixian_products_map_51.clear();

			JSONObject resp = searchYXCity(parentId);
			if (resp.getString("rescode").equals("00") && resp.containsKey("city")) {
				JSONArray arrays = JSONArray.fromObject(resp.getString("city"));
				for (int i = 0; i < arrays.size(); i++) {
					JSONObject obj = arrays.getJSONObject(i);
					String cityId = obj.get("id").toString();
					String divisionName = obj.get("divisionName").toString();
					if (!"0".equals(cityId)) {
//						String cityName = obj.get("divisionName").toString();
						JSONObject resp_ = searchYXCity(cityId);
						if (resp_.getString("rescode").equals("00") && resp_.containsKey("city")) {
							JSONArray arrays_ = JSONArray.fromObject(resp_.getString("city"));
							for (int i_ = 0; i_ < arrays_.size(); i_++) {
								JSONObject obj_ = arrays_.getJSONObject(i_);
								String cityId_ = obj_.get("id").toString();
								String cityName_ = obj_.get("divisionName").toString();
								// {"acqMerchantName": "利事环球酒店有限公司","acqMerchantNo": "1901151345441028413"},
								String resp__s = searchYXProduct(cityId_, acqcode);
								JSONArray resp__ = JSONArray.fromObject(resp__s);
								int s = 0;
								if (resp__ != null) {
									List<ProductBean> list = new ArrayList<>();
									ProductBean bean;
									JSONArray arrs__ = JSONArray.fromObject(resp__);
									s = arrs__.size();
									for (int j = 0; j < arrs__.size(); j++) {
										bean = new ProductBean();
										JSONObject oj = arrs__.getJSONObject(j);
										if (oj.containsKey("acqMerchantNo") && oj.containsKey("acqMerchantName")) {
											String acqMerchantNo = oj.get("acqMerchantNo").toString();
											String acqMerchantName = oj.get("acqMerchantName").toString();
											bean.setMcc(acqMerchantNo);
											bean.setName(acqMerchantName);
											list.add(bean);
										}
									}
									if (acqcode.equals(PropertiesConst.acq_code_yixian_xs8087))
										yixian_products_map.put(cityName_, list);
									if (acqcode.equals(PropertiesConst.acq_code_yixian_8063))
										yixian_products_map_51.put(cityName_, list);
								}
								System.out.println(acqcode+"【" + divisionName + "-" + cityName_ + "】商户" + s + "个");
							}
						}
					}
				}
			}
			if (acqcode.equals(PropertiesConst.acq_code_yixian_xs8087)) {
				Set<String> keySet = CityUtil.yixian_products_map.keySet();
				int count = 0;
				for (String key : keySet) {
					List list = CityUtil.yixian_products_map.get(key);
					count += list.size();
				}
				int size = CityUtil.yixian_products_map.size();
				System.out.println("刷新完毕，本次刷新亿贤新生城市共计" + size + "个，涉及商户共计" + count + "家！");
			}
			if (acqcode.equals(PropertiesConst.acq_code_yixian_8063)) {
				Set<String> keySet = CityUtil.yixian_products_map_51.keySet();
				int count = 0;
				for (String key : keySet) {
					List list = CityUtil.yixian_products_map_51.get(key);
					count += list.size();
				}
				int size = CityUtil.yixian_products_map_51.size();
				System.out.println("刷新完毕，本次刷新亿贤51酷宝城市共计" + size + "个，涉及商户共计" + count + "家！");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static JSONObject searchYXCity(String parentId) throws Exception, ClientProtocolException, IOException {
		String YKAgentNo = PropertiesConst.YIXIAN_AGENTNO;
		String YKAgentkey = PropertiesConst.YIXIAN_AES_PASS;
		Map<String, String> map = new HashMap<String, String>();
		map.put("parentId", parentId);
		map.put("agentNo", YKAgentNo);
		JSONObject object = JSONObject.fromObject(map);
		String dataMessage = Aes.llyEncrypt(object.toString(), YKAgentkey);
		Map<String, String> mapkey = new HashMap<String, String>();
		mapkey.put("agentNo", YKAgentNo);
		mapkey.put("IDStr", YKAgentkey);
		mapkey.put("dataMessage", dataMessage);
		mapkey.put("sign", MD5WSUtil.signData(mapkey, PropertiesConst.YIXIAN_MD5_PASS));
		String sr = HttpClientUtil.post(PropertiesConst.YIXIAN_URL_CITYSELECT, mapkey);
		JSONObject resp = JSONObject.fromObject(sr);
		return resp;
	}

	private static String searchYXProduct(String cityId, String acqcode) {
		try {
			String YKAgentkey = PropertiesConst.YIXIAN_AES_PASS;
			String YKAgentNo = PropertiesConst.YIXIAN_AGENTNO;
			Map<String, String> map = new HashMap<String, String>();
			map.put("bankAccount", "6225897656789871");
			map.put("acqCode", acqcode);
			map.put("agentNo", YKAgentNo);
			map.put("devisionId", cityId);
			JSONObject object = JSONObject.fromObject(map);
			String value = Aes.llyEncrypt(object.toString(), YKAgentkey);
			Map<String, String> map2 = new HashMap<String, String>();
			map2.put("agentNo", YKAgentNo);
			map2.put("IDStr", YKAgentkey);
			map2.put("dataMessage", value);
			map2.put("sign", MD5WSUtil.signData(map2, PropertiesConst.YIXIAN_MD5_PASS));
			String sr = HttpClientUtil.post(PropertiesConst.YIXIAN_URL_SLECTSHANGHU, map2);
			JSONObject resp = JSONObject.fromObject(sr);
			if (resp.getString("rescode").equals("00") && resp.containsKey("merchants")) {
				return resp.getString("merchants").toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		loadYxCityAndProducts("0", 8087 + "");
	}

}
